<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        li{
            list-style: none;
        }
        .banner_box{
            width: 600px;
            height: 400px;
            border:1px solid;
            margin: 100px auto;
            cursor: pointer;
            position: relative;
        }
        .banner_ul{
            width: 100%;
            height: 100%;
        }
        .banner_ul li{
            width: 100%;
            height: 100%;
            opacity: 0;
            position: absolute;
            left: 0;
            top: 0;
            transition: all 1s;
        }
        .banner_ul .active_banner{
            opacity: 1;
        }
        .banner_ul li img{
            width: 100%;
            height: 100%;
        }
        /* 上下按钮 */
        .prev_btn,.next_btn{
            position: absolute;
            width: 60px;
            height: 60px;
            top: 170px;
            font-size: 30px;
            font-family: '宋体';
            border: none;
            background: rgba(0,0,0,.3);
            color: white;
            border-radius: 50%;
            cursor: pointer;
            outline: none;
        }
        .prev_btn{
            left: 20px;
        }
        .next_btn{
            right: 20px;
        }
        /* 鼠标悬停 */
        .prev_btn:hover,.next_btn:hover{
            background: rgba(0,0,0,.6);
        }
    </style>
</head>
<body>
    <div class="banner_box">
        <ul class="banner_ul">
            <li class="active_banner">
                <img src="https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1603365312,3218205429&fm=26&gp=0.jpg" alt="">
            </li>
            <li>
                <img src="https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3557543371,89408500&fm=26&gp=0.jpg" alt="">
            </li>
            <li>
                <img src="https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2763274110,3993624877&fm=26&gp=0.jpg" alt="">
            </li>
            <li>
                <img src="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3942751454,1089199356&fm=26&gp=0.jpg" alt="">
            </li>
            <li>
                <img src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2005235653,1742582269&fm=26&gp=0.jpg" alt="">
            </li>
        </ul>
        <button class="prev_btn">&lt;</button>
        <button class="next_btn">&gt;</button>
    </div>
    <script>
        // 整个ul
        var banner_ul = document.getElementsByClassName('banner_ul')[0];
        // ul下的li
        var banner_ul_li = banner_ul.getElementsByTagName('li');
        // 上一页按钮
        var prev_btn = document.getElementsByClassName('prev_btn')[0];
        // 下一页按钮
        var next_btn = document.getElementsByClassName('next_btn')[0];
        //初始激活下标  
        var active_banner_index = 0;
        //定时器标识符
        var timer = null;
        prev_btn.onclick = function(){
            prev_pic()
        }
        next_btn.onclick = function(){
            next_pic()
        }
        //定时器
        timer = setInterval(next_pic,3000)
        //鼠标悬停，禁止计时
        banner_ul.onmouseover = function(){
            clearInterval(timer)
        }
        banner_ul.onmouseout = function(){
            timer = setInterval(next_pic,3000)
        }
        // 封装
        function next_pic(){
            active_banner_index++;
            if(active_banner_index > 4){
                active_banner_index = 0
            }
            for(var i=0;i<banner_ul_li.length;i++){
                banner_ul_li[i].className = '';
                banner_ul_li[active_banner_index].className = 'active_banner';
            }
        }
        function prev_pic(){
            active_banner_index--;
            if(active_banner_index < 0){
                active_banner_index = 4
            }
            for(var i=0;i<banner_ul_li.length;i++){
                banner_ul_li[i].className = '';
                banner_ul_li[active_banner_index].className = 'active_banner';
            }
        }
    </script>
</body>
</html>